#include "App.h"

#define TIMER_LENGTH 15

#ifndef O_KEY_UP
	#define O_KEY_UP	250
	#define O_KEY_DOWN	251
	#define O_KEY_LEFT	252
	#define O_KEY_RIGHT 253
#endif

#if defined(__APPLE__) || defined(MACOSX)
	   #include <GLUT/glut.h>
#endif
#if defined(_WIN32)
	#include <GL/glut.h>
#endif
#include "Defines.h"

void paintGL (){App->paintGL();glutSwapBuffers();}
void timer (int x){App->subTimer(1.0);paintGL(); glutTimerFunc(TIMER_LENGTH,timer,0);}
void keyFunc (unsigned char c, int x, int y){App->keyPress(c,glutGetModifiers());}
void keyUpFunc (unsigned char c, int x, int y){App->keyRelease(c,glutGetModifiers());}
void specialFunc(int key, int x, int y)
{
	int k = key;
	if (k == GLUT_KEY_LEFT){k = O_KEY_LEFT;}
	else if (k == GLUT_KEY_RIGHT){k = O_KEY_RIGHT;}
	else if (k == GLUT_KEY_UP){k = O_KEY_UP;}
	else if (k == GLUT_KEY_DOWN){k = O_KEY_DOWN;}
	
	unsigned int modFlags = glutGetModifiers();
	unsigned int sModFlags = 0;
	if (modFlags & GLUT_ACTIVE_CTRL){ sModFlags |= Y::CTRL; }
	if (modFlags & GLUT_ACTIVE_SHIFT){ sModFlags |= Y::SHIFT; }
	if (modFlags & GLUT_ACTIVE_ALT){ sModFlags |= Y::ALT; }
	
	App->keyPress(k,sModFlags);
}

void specialUpFunc(int key, int x, int y)
{
	unsigned char k = key;
	if (k == GLUT_KEY_LEFT){k = O_KEY_LEFT;}
	else if (k == GLUT_KEY_RIGHT){k = O_KEY_RIGHT;}
	else if (k == GLUT_KEY_UP){k = O_KEY_UP;}
	else if (k == GLUT_KEY_DOWN){k = O_KEY_DOWN;}
	
	unsigned int modFlags = glutGetModifiers();
	unsigned int sModFlags = 0;
	if (modFlags & GLUT_ACTIVE_CTRL){ sModFlags |= Y::CTRL; }
	if (modFlags & GLUT_ACTIVE_SHIFT){ sModFlags |= Y::SHIFT; }
	if (modFlags & GLUT_ACTIVE_ALT){ sModFlags |= Y::ALT; }
	
	App->keyRelease(k,sModFlags);
}

void mouseFunc(int button, int state, int x, int y){App->keyModifiers = glutGetModifiers();	App->mouseButton = button;
	if (state == GLUT_DOWN){App->mousePress(x,y);}else{App->mouseRelease(x,y);}}
void mouseUpFunc(int x, int y){App->mouseRelease(x,y);}
void passiveMotionFunc(int x, int y){App->mouseMove(x,y);}
void motionFunc(int x, int y){App->mouseDrag(x,y);}
void resizeFunc(int x, int y){App->resizeGL(x,y);}
